﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class LoaiVanBanBUS
    {
        public void themLoaiVanBan(LoaiVanBan lvbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.LoaiVanBans.InsertOnSubmit(lvbDTO);

            storeDB.SubmitChanges();
        }

        public void suaLoaiVanBan(LoaiVanBan lvbDTO, string maLoaiVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiVanBans.Where(q => q.MaLoaiVanBan == maLoaiVanBan).First();
            query.TenLoaiVanBan = lvbDTO.TenLoaiVanBan;

            storeDB.SubmitChanges();
        }

        public void xoaLoaiVanBan(GridView gridView)
        {
            //Lay MaLoaiVanBan duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaLoaiVanBan"));

                if (_checkBox.Checked == true)
                {
                    string maLoaiVanBanCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maLoaiVanBanCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaLoaiVanBan duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length>0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));
               
                var query = storeDB.LoaiVanBans.Where(q => q.MaLoaiVanBan == maTemp).First();

                storeDB.LoaiVanBans.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachLoaiVanBan();
            gridView.DataBind();
        }

        public IList<LoaiVanBan> layDanhSachLoaiVanBan()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstLoaiVanBan = storeDB.LoaiVanBans.ToList();

            return lstLoaiVanBan;
        }

        public Boolean kiemtraTonTaiMaLoaiVanBan(string maLoaiVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiVanBans.Where(q => q.MaLoaiVanBan == maLoaiVanBan).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenLoaiVanBan(string tenLoaiVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiVanBans.Where(q => q.TenLoaiVanBan == tenLoaiVanBan).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaLoaiVanBan(string maLoaiVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maLoaiVanBan = storeDB.LoaiVanBans.Where(m => m.MaLoaiVanBan == maLoaiVanBan).Select(m =>  m.MaLoaiVanBan).First();

            return _maLoaiVanBan;
        }

        public string layTenLoaiVanBan(string maLoaiVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenLoaiVanBan = storeDB.LoaiVanBans.Where(m => m.MaLoaiVanBan == maLoaiVanBan).Select(m => m.TenLoaiVanBan).First();

            return tenLoaiVanBan;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiVanBans.ToList();
            foreach (var q in query)
            {
                var maLoaiVanBan = q.MaLoaiVanBan;
                var tenLoaiVanBan = q.TenLoaiVanBan;

                ListItem item = new ListItem();
                item.Text = tenLoaiVanBan;
                item.Value = maLoaiVanBan;

                dropDownList.Items.Add(item);
            }
        }
    }
}
